/**    
 * @{#} RegisterActivity.java Create on 2014-5-26 下午9:51:05    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0
 */
package com.smiier.skin;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.skinapp.R;

import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.UserRegistTask;
import com.smiier.skin.net.UserRegistTask.UserRegistRequest;
import com.smiier.skin.net.UserRegistTask.UserRegistResponse;
import com.smiier.skin.net.UserSetPwdTask;
import com.smiier.skin.net.UserSetPwdTask.UserSetPwdRequest;
import com.smiier.skin.net.UserSetPwdTask.UserSetPwdResponse;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class RegisterThirdStepActivity extends BasicActivity {

	private EditText edit_password;
	private String sms_code, tel;

	private int identity;
	private Button btn_submit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_3);
		init();
		setNavLeftBtn("取消");
		setNavTitle("设置密码");
		setNavLeftBtnNoDrawable();

		identity = getIntent().getIntExtra(Constant.IDENTITY_KEY_1, 0);

		tel = getIntent().getStringExtra(Constant.IDENTITY_KEY);
		sms_code = getIntent().getStringExtra(Constant.IDENTITY_KEY_SMS);
		edit_password = (EditText) findViewById(R.id.edit_password);
		btn_submit = (Button) findViewById(R.id.btn_submit);
		if (identity == Constant.TYPE_FORGET_PASSWORD
				|| identity == Constant.TYPE_UPDATE_PASSWORD) {
			btn_submit.setText("提交");
		}
	}

	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.btn_submit) {
			String password = edit_password.getText().toString();
			if (!CommonUtility.isNull(tel) && !CommonUtility.isNull(sms_code)) {
				if (password.length() < 8 || password.length() > 16) {
					// CommonUtility.tip(this, "密码长度为6-18。");
					CommonUtility.tip(this, "8-16位数字和数字");
					return;
				}
				if (identity == Constant.TYPE_FORGET_PASSWORD
						|| identity == Constant.TYPE_UPDATE_PASSWORD) {
					UserSetPwdRequest req = new UserSetPwdRequest();
					req.mobile = tel;
					req.pwd = password;
					req.confirmCode = sms_code;
					UserSetPwdTask task = new UserSetPwdTask();
					task.setRequest(req);
					task.addListener(new NetTaskListener<UserSetPwdTask.UserSetPwdRequest, UserSetPwdTask.UserSetPwdResponse>() {

						@Override
						public void onException(
								INetTask<UserSetPwdRequest, UserSetPwdResponse> task,
								Exception e) {
							toast(R.string.server_unavailable);
						}

						@Override
						public void onComplete(
								INetTask<UserSetPwdRequest, UserSetPwdResponse> task,
								UserSetPwdResponse response) {
							if (response.ResultCode != 200) {
								toast("操作失败");
								return;
							}
							CommonUtility.finish();
							startActivity(new Intent(getContext(),
									LoginActivity.class));
							toast("密码设置成功");
						}
					});
					add(task);
				} else {
					UserRegistRequest req = new UserRegistRequest();
					req.mobile = tel;
					req.pwd = password;
					req.confirmCode = sms_code;
					UserRegistTask task = new UserRegistTask();
					task.setRequest(req);
					task.addListener(new NetTaskListener<UserRegistTask.UserRegistRequest, UserRegistTask.UserRegistResponse>() {

						@Override
						public void onException(
								INetTask<UserRegistRequest, UserRegistResponse> task,
								Exception e) {
							toast(R.string.server_unavailable);
						}

						@Override
						public void onComplete(
								INetTask<UserRegistRequest, UserRegistResponse> task,
								UserRegistResponse response) {
							if (response.ResultCode != 200) {
								toast("操作失败");
								return;
							}
							
							
							Log.i("testtoken", "RegisterThirdStepActivity.response.Res.Token"+(response.Res.Token));
							
							
							GlobalSettings.sToken = response.Res.Token;
							GlobalSettings.sUid = response.Res.Uid;
							Intent intent = new Intent(getContext(),
									DoctorProfileEditActivity.class);
							intent.putExtra(Constant.IDENTITY_KEY, true);
							startActivity(intent);
							// startActivity(new Intent(getContext(),
							// RegisterFourthStepActivity.class));
						}
					});
					add(task);
				}
			} else {
				toast("操作失败");
			}
		} else if (id == R.id.btn_left) {
			finish();
			CommonUtility
					.finishActivityFromName(RegisterFirstStepActivity.class
							.getSimpleName());
			CommonUtility
					.finishActivityFromName(RegisterSecondStepActivity.class
							.getSimpleName());
		}
	}
	
	
	/*@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
